<template>
  <div>
    <h1>用户管理</h1>
    <!-- 多条件模糊查询 -->
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="Approved by"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit()">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 批量删除按钮 -->
    <el-button
      type="danger"
      @click="onDeleteBatch"
      :disabled="multipleSelection.length == 0"
      >批量删除</el-button
    >
    <!-- 表格 -->
    <div>
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" :selectable="selectable" />
        <el-table-column prop="userId" label="用户Id" />
        <el-table-column prop="userName" label="用户名" />
        <el-table-column prop="nickName" label="真实姓名" />
        <el-table-column prop="userMobile" label="手机号" />
        <el-table-column prop="userEmail" label="邮箱" />
        <el-table-column prop="userAvatar" label="头像">
          <template #default="scope">
            <img
              v-if="scope.row.userAvatar != null"
              :src="scope.row.userAvatar"
              alt=""
              style="width: 50px; height: 50px; border-radius: 50%"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="注册时间" />
        <el-table-column prop="userStatus" label="审核状态">
          <template #default="scope">
            <el-tag v-if="scope.row.userStatus == 0">待审核</el-tag>
            <el-tag type="success" v-if="scope.row.userStatus == 1"
              >审核通过</el-tag
            >
            <el-tag type="danger" v-if="scope.row.userStatus == 2"
              >审核驳回</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="reason" label="审核理由" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              v-if="scope.row.userStatus == 0"
              @click="OnCheckUser(scope.row)"
              >审核</el-button
            >
            <el-button
              type="success"
              size="small"
              @click="OnCheckUser(scope.row)"
              >修改</el-button
            >
            <el-button
              type="danger"
              size="small"
              @click="onDeleteUser(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div>
      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[2, 3, 4, 5]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 审核对话框 -->
    <div>
      <el-dialog v-model="dialogFormVisible" title="用户审核" width="500">
        <el-form :model="form">
          <el-form-item label="用户账号" :label-width="formLabelWidth">
            <el-input v-model="form.userName" autocomplete="off" disabled />
          </el-form-item>
          <el-form-item label="用户姓名" :label-width="formLabelWidth">
            <el-input v-model="form.nickName" autocomplete="off" disabled />
          </el-form-item>
          <el-form-item label="用户手机号" :label-width="formLabelWidth">
            <el-input v-model="form.userMobile" autocomplete="off" disabled />
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="form.userEmail" autocomplete="off" disabled />
          </el-form-item>
          <el-form-item label="审核状态" :label-width="formLabelWidth">
            <el-radio-group v-model="form.userStatus">
              <el-radio :value="1" size="large">审核通过</el-radio>
              <el-radio :value="2" size="large">审核驳回</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审核说明" :label-width="formLabelWidth">
            <el-input
              type="textarea"
              v-model="form.reason"
              autocomplete="off"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="onSubmitCheckUser">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from "element-plus";
import { onMounted, ref } from "vue";
import { checkUser, deleteUser, getUserList,deleteUserBatch } from "../api/api";
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: "",
});
const total = ref(0);
const tableData = ref([]);
const form = ref({
  userId: null,
  userStatus: null,
  reason: null,
  userName: null,
  nickName: null,
  userMobile: null,
  userEmail: null,
});
const multipleSelection = ref<any>([]);
// 多选回调函数
const handleSelectionChange = (val: any) => {
  multipleSelection.value = val;
};

// 批量删除
const onDeleteBatch = () => {
  ElMessageBox.confirm("数据删除后无法恢复，你确定要删除吗？", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "放弃",
    type: "warning",
  })
    .then(() => {
      deleteUserBatch(multipleSelection.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};

// 单个删除用户方法
const onDeleteUser = (row: any) => {
  ElMessageBox.confirm("数据删除后无法恢复，你确定要删除吗？", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "放弃",
    type: "warning",
  })
    .then(() => {
      deleteUser(row.userId).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};

// 提交审核方法
const onSubmitCheckUser = () => {
  checkUser(form.value).then((res) => {
    if (res.data.code == 200) {
      dialogFormVisible.value = false;
      ElMessage.success("审核成功");
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

// 审核用户显示弹框方法
const OnCheckUser = (row: any) => {
  dialogFormVisible.value = true;
  Object.assign(form.value, row);
};

// 模糊查询方法
const onSubmit = () => {
  loadList();
};
//每页条数触发条件
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
// 当前页数触发
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};
// 调用api方法查询用户列表赋值
const loadList = () => {
  getUserList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
// 钩子函数页面启动自动调用方法
onMounted(() => {
  loadList();
});
</script>

<style lang="scss" scoped></style>
